//
//  ArtRoadshowTour.m
//  SmartRoadShow
//
//  Created by  ibokan on 10-9-25.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "ArtRoadshowTour.h"
#import "database.h"
#import "DateUtil.h"

@implementation ArtRoadshowTour
@synthesize roadshow_id,_city,_starttime,_endtime,_position,_description;

+(NSArray *) searchAllArtRoadshowTour:(NSString *)dateString
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	NSMutableArray *artRoadshowArray = [[NSMutableArray alloc] init];
	
	rs = [db executeQuery:@"select * from ArtRoadshowTour"];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		int roadshowId = [[rs objectForColumn:@"roadshow_id"] intValue];
		NSString *city = [rs objectForColumn:@"city"];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *position = [rs objectForColumn:@"position"];
		NSString *description = [rs objectForColumn:@"description"];
		
		if([DateUtil diffBetweenDateStr:dateString andDateStr:starttime] && [DateUtil diffBetweenDateStr:starttime andDateStr:dateString])
		{
			ArtRoadshowTour *artRoadshow = [[ArtRoadshowTour alloc] initWithID:ID 
																	RoadshowId:roadshowId 
																		  City:city 
																	 Starttime:starttime 
																	   Endtime:endtime 
																	  Position:position 
																   Description:description];
			[artRoadshowArray addObject:artRoadshow];
			[artRoadshow release];
		}
	}
	[rs close];
	
	return [artRoadshowArray autorelease];
}

+(BOOL) IsExistArtRoadshowTour:(NSString *)dateString
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select starttime,endtime from ArtRoadshowTour"];
	while ([rs next])
	{
		NSString *start = [rs objectForColumn:@"starttime"];
		NSString *end  = [rs objectForColumn:@"endtime"];
		
		if([DateUtil diffBetweenDateStr:dateString andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:dateString])
		{
			return YES;
		}
	}
	return NO;
}


+(BOOL)findRoadshowId:(int)roadShowId
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select roadshow_id from ArtRoadshowTour"];
	while ([rs next])
	{
		int roadshowID = [[rs objectForColumn:@"roadshow_id"] intValue];
		if(roadShowId == roadshowID)
		{
			return YES;
		}
	}
	return NO;
}


+(NSArray *) findAll
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select * from ArtRoadshowTour"];
	NSMutableArray *artRoadshowArray = [[NSMutableArray alloc] init];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		int roadshowId = [[rs objectForColumn:@"roadshow_id"] intValue];
		NSString *city = [rs objectForColumn:@"city"];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *position = [rs objectForColumn:@"position"];
		NSString *description = [rs objectForColumn:@"description"];
		
	ArtRoadshowTour *artRoadshow = [[ArtRoadshowTour alloc] initWithID:ID RoadshowId:roadshowId City:city Starttime:starttime Endtime:endtime Position:position Description:description];
		[artRoadshowArray addObject:artRoadshow];
		[artRoadshow release];
	}
	
	[rs close];
	
	return [artRoadshowArray autorelease];
}

+(int) insertWithRoadshowId:(int)roadshowId City:(NSString *)city Starttime:(NSString *)start Endtime:(NSString *)end Position:(NSString *)posi Description:(NSString *)description
{
	PLSqliteDatabase *db = [database setup];
   BOOL re =[db executeUpdate:@"insert into ArtRoadshowTour(roadshow_id,city,starttime,endtime,position,description) values(?,?,?,?,?,?)",
			 [NSNumber numberWithInt:roadshowId],
			 city,
			 start,
			 end,
			 posi,
			 description];
	
    return re;
}

+(int)delete:(int)Id
{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re = [db executeUpdate:@"delete from ArtRoadshowTour where ID = ?",[NSNumber numberWithInt:Id]];
	
	return re;
}


-(id) initWithID:(int)Id RoadshowId:(int)roadshowId City:(NSString *)city Starttime:(NSString *)start Endtime:(NSString *)end Position:(NSString *)posi Description:(NSString *)description
{
	if(self = [super init])
	{
		_id = Id;
		roadshow_id = roadshowId;
		_city = [city retain];
		_starttime = [start retain];
		_endtime = [end retain];
		_position = [posi retain];
		_description = [description retain];
	}
	
	return self;
}

-(int) Id
{
	return _id;
}

-(int) roadshowId
{
	return roadshow_id;
}

-(NSString *)city
{
	return _city;
}

-(NSString *) starttime
{
	return _starttime;
}

-(NSString *)endtime
{
	return _endtime;
}

-(NSString *)position
{
	return _position;
}

-(NSString *) description
{
	return _description;
}


@end
